package com.google.appengine.api.datastore;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 *
 * @author Secondary User
 */
@RequiredArgsConstructor
public class FetchOptions
{
	@Getter
	private final int chunkSize;
	@Getter
	private final Cursor startAt;
	@Getter
	private final Cursor endAt;
	@Getter
	private final int limit;
	@Getter
	private final int offset;

	public FetchOptions startCursor(Cursor startAt)
	{
		return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
	}

	public FetchOptions endCursor(Cursor endAt)
	{
		return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
	}

	public FetchOptions limit(int limit)
	{
		return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
	}

	public FetchOptions offset(int offset)
	{
		return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
	}

	public FetchOptions chunkSize(int chunkSize)
	{
		return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
	}

	@RequiredArgsConstructor
	public static class Builder
	{

		private final int chunkSize;
		private final Cursor startAt;
		private final Cursor endAt;
		private final int limit;
		private final int offset;
		private static final int DEFAULT_CHUNKSIZE = 30;
		private static final Cursor DEFAULT_STARTAT = null;
		private static final Cursor DEFAULT_ENDAT = null;
		private static final int DEFAULT_LIMIT = 10000;
		private static final int DEFAULT_OFFSET = 0;

		public static FetchOptions.Builder withStartCursor(Cursor startAt)
		{
			return new FetchOptions.Builder(DEFAULT_CHUNKSIZE, startAt, DEFAULT_ENDAT, DEFAULT_LIMIT, DEFAULT_OFFSET);
		}

		public static FetchOptions withDefaults()
		{
			return new FetchOptions(DEFAULT_CHUNKSIZE, DEFAULT_STARTAT, DEFAULT_ENDAT, DEFAULT_LIMIT, DEFAULT_OFFSET);
		}

		public FetchOptions.Builder offset(int offset)
		{
			return new FetchOptions.Builder(DEFAULT_CHUNKSIZE, DEFAULT_STARTAT, DEFAULT_ENDAT, DEFAULT_LIMIT, offset);
		}

		public FetchOptions limit(int i)
		{
			return new FetchOptions(chunkSize, startAt, endAt, limit, offset);
		}
	}
}
